package com.example.demo.web;

import com.example.demo.dao.BookMonthMapper;
import com.example.demo.dao.BookUserMapper;
import com.example.demo.dao.UserMapper;
import com.example.demo.entity.BookMonth;
import com.example.demo.entity.BookUser;
import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("web")
@Controller
public class UserControl {
    @Autowired
    UserMapper userMapper;
    @Autowired
    BookUserMapper bookUserMapper;
    @Autowired
    BookMonthMapper bookMonthMapper;

    @RequestMapping("book")
    @ResponseBody
    public List<BookUser> book(){
        List<BookUser> list=bookUserMapper.list1();
        return list;
    }
    @RequestMapping("month")
    @ResponseBody
    public List<BookMonth> month(){
        List<BookMonth> list=bookMonthMapper.list2();
        return list;
    }

    @RequestMapping("list")
    @ResponseBody
    public List<User> list(){
        List<User> list=userMapper.list();
        return list;
    }


    @RequestMapping("listhtml")
    public String listhtml(ModelMap map){
        List<User> list=userMapper.list();
        for (User user : list) {
            System.out.println(user.getId());
            System.out.println(user.getName());
            System.out.println(user.getPosition());
            System.out.println(user.getOffice());
            System.out.println(user.getAge());
        }
        map.put("userlist",list);
        return "/user/tables";
    }

    @RequestMapping("delete/{id}")
    public String delete(@PathVariable("id") String xxx){
        userMapper.delete(xxx);
        return "redirect:/web/listhtml";
    }

    @RequestMapping("add")
    public String add(User user){
        return "/user/add";
    }
    @PostMapping("save")
    public String save(User user){
        userMapper.save(user);
        return "redirect:/web/listhtml";
    }
    @GetMapping("edit/{id}")
    public String edit(@PathVariable("id") String id,ModelMap map){
        User user=userMapper.cel(id);
        map.put("user1",user);
        return "/user/edit";
    }
    @RequestMapping("update")
    public String update(User user){
        userMapper.update(user);
        return "redirect:/web/listhtml";
    }
}
